import axios from 'axios';
import utils from '~/plugins/utils';

import HTTP_PATH from '~/static/server.config'

export const state = () => ({
    levelOptions: [],
    lessonOptions: {},
    //tools.vue
    gridTemplateList: [],
    //courseList.vue
    courseList: [],
    learnerOptions: [],
    //courseDetail.vue
    courseDetail: [],
    courseTemplate: [],
    exerciseTemplate: [],
    //myCourse.vue
    myCourseList: [],
    //courseProgress.vue
    courseProgressList: [],
    myClassOptions: [],
    myClassStudentOptions: [],
    //exercisesProgress.vue
    classExerciseProgressList: [],
    myExerciseProgressList: [],
    //courseRecords.vue
    courseRecordsList: [],
    //exerciseRecords.vue
    exerciseRecordsList: [],
    //homework
    homeworkList: [],
    checkOptions: [],
    homeworkDetail: [],
    myClassHomeworkDetail:[],
    submitHomework:[],
    feedback:[],
    checkHomework: [],
    handOutUrl:[]
});
export const mutations = {
    GET_MY_STUDY_LEVEL(state, data){
        let option = [];
        for (let key in data) {
            const item = {value: data[key].id, label: data[key].name};
            const lesson = data[key].lessons;
            const lessonArr = [];
            for (let _key in lesson) {
                lessonArr.push({value: lesson[_key].id, label: lesson[_key].name});
            }
            state.lessonOptions[data[key].id] = lessonArr;
            option.push(item);
        }
        state.levelOptions = option;
    },
    //tools.vue
    GET_GRID_TEMPLATE_LIST(state, data){
        state.gridTemplateList = data;
    },
    //courseList.vue
    GET_COURSE_LIST(state, data){
        state.courseList = data;
    },
    GET_LEARNER_OPTIONS(state, data){
        let option = [];
        for (let key in data) {
            option.push(data[key]);
        }
        state.learnerOptions = option;
    },
    //courseDetail.vue
    GET_COURSE_DETAIL(state, data){
        state.courseDetail = data;
    },
    SET_COURSE_TEMPLATE(state, data){
        state.courseTemplate = data;
    },
    SET_EXERCISE_TEMPLATE(state, data){
        state.exerciseTemplate = data;
    },
    //myCourse.vue
    GET_MY_COURSE_LIST(state, data){
        state.myCourseList = data;
    },
    //courseProgress.vue
    GET_COURSE_PROGRESS_LIST(state, data){
        state.courseProgressList = data;
    },
    GET_CLASS_STUDENT_LIST(state, data) {
        let option = [];
        for (let key in data) {
            const classItem = {value: data[key].id, label: data[key].name};
            const students = data[key].student_info;
            const stuArr = [];
            for (let _key in students) {
                stuArr.push({value: students[_key].id, label: students[_key].user_name});
            }
            state.myClassStudentOptions[data[key].id] = stuArr;
            option.push(classItem);
        }
        state.myClassOptions = option;
    },
    GET_CLASS_EXERCISE_PROGRESS_LIST(state, data) {
        state.classExerciseProgressList = data;
    },
    GET_MY_EXERCISE_PROGRESS_LIST(state, data) {
        state.myExerciseProgressList = data;
    },
    //courseRecords.vue
    GET_COURSE_RECORDS_LIST(state, data) {
        state.courseRecordsList = data;
    },
    //exerciseRecords.vue
    GET_EXERCISE_RECORDS_LIST(state, data) {
        state.exerciseRecordsList = data;
    },
    GET_HOMEWORK_LIST(state, data) {
        state.homeworkList = data;
    },
    GET_CHECK_STATUS(state, data) {
        let option = [];
        for (let key in data) {
            option.push({value: key, label: data[key]});
        }
        state.checkOptions = option;
    },
    GET_HOMEWORK_DETAIL(state, data) {
        state.homeworkDetail = data;
    },
    GET_MY_CLASS_HOMEWORK_DETAIL(state, data){
        state.myClassHomeworkDetail = data;
    },
    GET_SUBMIT_HOMEWORK(state, data){
        state.submitHomework = data;
    },
    GET_FEEDBACK(state, data){
        state.feedback = data;
    },
    GET_CHECKHOMEWORK(state, data){
        state.checkHomework = data;
    },
    //courseDetail.vue
    GET_HANDOUTURL(state, data){
        state.handOutUrl = data;
    }
};
export const actions = {
    async getMyStudyLevel({state, commit, rootState}, paramData){
        try {
            let res = await utils.sendAxiosPost("/api/study/getMyStudyLevel",
                    {uid: rootState.authUser.uid,token:rootState.authUser.token});
            commit("GET_MY_STUDY_LEVEL", res.data.data);
        } catch (e) {
            console.error(e);
        }
    },
    //tools.vue
    async getGridTemplateList({state, commit, rootState}, paramData){
        try {
            commit("GET_GRID_TEMPLATE_LIST", (await utils.sendAxiosPost("/api/tools/getTinWordTemplate", 
                {uid: rootState.authUser.uid,token: rootState.authUser.token, ...paramData})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    //courseList.vue
    async getCourseList({commit, rootState}, params){
        let res = await utils.sendAxiosPost("/api/study/getMyCourse",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params});
        commit("GET_COURSE_LIST", res.data.data)
    },
    async getLearnerOptions({state, commit, rootState}, paramData){
        try {
            let res = await utils.sendAxiosGet("/api/getLearnerOption",'','');
            commit("GET_LEARNER_OPTIONS", res.data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    //courseDetail.vue
    async getCourseDetail({state, commit, rootState}, param){
        try {
            state.courseDetail.length || commit("GET_COURSE_DETAIL",
                (await utils.sendAxiosPost("/api/study/getMyStudyDetail",
                    {uid: rootState.authUser.uid,token:rootState.authUser.token, ...param})).data.data)
        } catch (e) {
            console.error(e);
        }
    },
    async getExerciseTemplate({state, commit, rootState}, param){
        commit("SET_EXERCISE_TEMPLATE", (await utils.sendAxiosPost("/api/study/getExerciseTemplate",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...param})).data.data);
    },
    async getCourseTemplate({state, commit, rootState}, param){
        commit("SET_COURSE_TEMPLATE", (await utils.sendAxiosPost("/api/study/getCourseTemplate", 
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...param})).data.data);
    },
    //myCourse.vue
    async getMyCourseList({state, commit, rootState}){
        try {
            state.myCourseList.length || commit("GET_MY_COURSE_LIST",
                (await utils.sendAxiosPost("/api/learner/getMyLevel", 
                    {uid: rootState.authUser.uid,token:rootState.authUser.token})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    //courseProgress.vue
    async getCourseProgressList({commit, rootState}, params){
        let res = await utils.sendAxiosPost("/api/progress/getClassCourseProgress",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params});
        commit("GET_COURSE_PROGRESS_LIST", res.data.data);
    },
    async getClassStudentList({commit, rootState, state}, paramData){
        commit("GET_CLASS_STUDENT_LIST",
            (await utils.sendAxiosPost("/api/educator/getMyClassSP",
                {uid: rootState.authUser.uid,token:rootState.authUser.token})).data.data);
    },
    //educator/exerciseProgress.vue
    async getClassExerciseProgressList({state, commit, rootState}, params){
        try {
            commit("GET_CLASS_EXERCISE_PROGRESS_LIST", (await utils.sendAxiosPost("/api/progress/getClassExerciseProgress", 
                {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    //learner/exerciseProgress.vue
    async getMyExerciseProgressList({state, commit, rootState}, params){
        try {
            commit("GET_MY_EXERCISE_PROGRESS_LIST", (await utils.sendAxiosPost("/api/progress/getExerciseProgress", 
                {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    //courseRecord.vue
    async getCourseRecordsList({state, commit, rootState}, params){
        try {
            commit("GET_COURSE_RECORDS_LIST", (await utils.sendAxiosPost("/api/progress/getCourseRecord",
                {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    //exercisesRecords
    async getExerciseRecordsList({state, commit, rootState}, params){
        try {
            commit("GET_EXERCISE_RECORDS_LIST", (await utils.sendAxiosPost("/api/progress/getExerciseRecord",
                {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    async getHomeworkList({state, commit, rootState}, params) {
        try {
            commit("GET_HOMEWORK_LIST", (await utils.sendAxiosPost("/api/homework/getHomeworkList",
                {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params})).data.data || []);
        } catch (e) {
            console.error(e);
        }
    },
    async getCheckStatus({state, commit, rootState}, params) {
        try {
            commit("GET_CHECK_STATUS", (await utils.sendAxiosGet("/api/getHomeworkOption", ['token'], params)).data.data.check_status);
        } catch (e) {
            console.error(e);
        }
    },
    async getHomeworkDetail({state, commit, rootState}, param){
        try {
            state.homeworkDetail.length || commit("GET_HOMEWORK_DETAIL",
                (await utils.sendAxiosPost("/api/homework/getHomeworkDetail",
                    {uid: rootState.authUser.uid,token:rootState.authUser.token, ...param})).data.data)
        } catch (e) {
            console.error(e);
        }
    },
    async myClassHomeworkDetail({state, commit, rootState}, param){
        try {
            state.myClassHomeworkDetail.length || commit("GET_MY_CLASS_HOMEWORK_DETAIL",
                (await utils.sendAxiosPost("/api/homework/getHomeworkDetail",
                    { uid: rootState.authUser.uid,token:rootState.authUser.token,...param})).data.data)
        } catch (e) {
            console.error(e);
        }
    },
    async submitHomework({commit, rootState}, params){
        let res = await utils.sendAxiosPost("/api/homework/submitHomework",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params});
        return res;
    },
    async feedback({commit, rootState}, params){
        let res = await utils.sendAxiosPost("/api/homework/feedbackHomework",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params});
        return res;
    },
    async checkHomework({commit, rootState}, params){
        let res = await utils.sendAxiosPost("/api/homework/checkedHomework",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params});
        return res;
    },
    //courseDetail.vue
    async getDownLoadHandOut({commit, rootState}, params){
        let res = await utils.sendAxiosPost("/api/tools/getDownLoadHandOut",
            {uid: rootState.authUser.uid,token:rootState.authUser.token, ...params});
        commit("GET_HANDOUTURL", res.data.data);
    }
}